import { size, IFormProps, pageSize, dateTimeFormat } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Button, Input, Table, Select } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
import moment from 'moment';
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';
import HomeSettingEdit from "../../components/storeSettings/HomeSettingEdit";

export interface IHomeSettingsPros extends IFormProps {
    storeSetting: any
}
class HomeSettings extends React.PureComponent<IHomeSettingsPros, any>{
    constructor(props) {
        super(props);
        this.state = {}
    }

    search = () => {
        this.getHomeSettings();
    }

    getHomeSettings = (payload?) => {
        payload = Object.assign({}, this.props.storeSetting.homeSettingsSearchs, this.props.form.getFieldsValue(), { ...payload });
        this.props.dispatch({
            type: "storeSetting/getHomeSettings",
            payload
        });
    }

    reset = () => {
        this.props.form.resetFields();
    }

    edit = (homeSetting = {}) => {
        this.props.dispatch({
            type: "storeSetting/save",
            payload: { homeSetting }
        });
        this.setVisible(true);
    }

    setVisible = (visible = false) => {
        this.setState({ visible });
    }

    save = (form) => {
        this.props.dispatch({
            type: "storeSetting/saveHomeSetting",
            payload: {
                form,
                callback: () => {
                    this.setVisible();
                }
            }
        });
    }

    render(): JSX.Element {
        //console.log(this.props);
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const { homeSetting, homeSettings: { totalCount, items }, homeSettingsSearchs: { pageIndex, pageSize, keywords } } = this.props.storeSetting;
        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.storeSetting.homeSettingsSearchs,
                { pageIndex, pageSize });
            this.getHomeSettings(payload);
        }

        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10", "20", "30", "50"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };

        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                    <Form layout="inline">
                    <Form.Item>
                        <Button size={size} onClick={() => this.edit()}>新增</Button>
                        </Form.Item>
                    <Form.Item>
                        </Form.Item>
                            <Form.Item>
                                {getFieldDecorator("keywords", {
                                    initialValue: keywords
                                })(<Input
                                    style={{ width: 200 }}
                                    size={size}
                                    placeholder="输入设置描述"
                                />)}
                            </Form.Item>
                            <Form.Item>
                                <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                            </Form.Item>
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>主页设置列表</h3>
                        </Col>
                        <Col span={24}>
                            <Table
                                dataSource={items}
                                bordered
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallStoreSettingsVOHomeSettingVO) => String(record.id)}
                                loading={loading}
                            >
                                <Table.Column title="序号" key="no" render={(text, record, index) => {
                                    return index + 1
                                }} />
                                <Table.Column title="描述" key="description" dataIndex="description" />
                                <Table.Column title="字体颜色" key="color" dataIndex="color" />
                                <Table.Column title="字体大小（单位rem）" key="fontSize" dataIndex="fontSize" />
                                <Table.Column title="操作" render={(text, record: api.SingleUserMallStoreSettingsVOHomeSettingVO, index) => {
                                    return (
                                        <div>
                                            <a className={Styles.M_RIGHT} onClick={() => this.edit(record)}>
                                                编辑
                                            </a>
                                        </div>
                                    );
                                }} />
                            </Table>
                        </Col>
                    </Col>
                </Row>
                {
                    this.state.visible ? (<HomeSettingEdit visible={this.state.visible}
                        homeSetting={homeSetting}
                        onSave={this.save}
                        onCancel={this.setVisible} />) :
                        (undefined)
                }
            </Row>)
    }
}

export default connect(({ storeSetting, loading: { models: { storeSetting: loading } } }) => ({ storeSetting, loading }))(Form.create()(HomeSettings));
